<template>
	<view>
		<view class="headerCon">
			<u-field v-model="search.type" label="型号" placeholder="请输入型号"><u-button size="mini" slot="right" type="success" @click="searchClick">搜索</u-button></u-field>
		</view>
		<view class="tableCon"><InStorageItem :list="list" :need-sum="true" :col="tableCol" @sort-change="sortChange"></InStorageItem></view>
		<u-back-top :scroll-top="scrollTop"></u-back-top>
	</view>
</template>

<script>
import { mapActions } from 'vuex';
import InStorageItem from './components/storageListItem/inStorageItem.vue';
export default {
	components: {
		InStorageItem
	},
	data() {
		return {
			scrollTop: 0,
			search: {
				type: ''
			},
			list: [],
			originList: [],
			tableCol: [
				{
					label: '机身码',
					key: 'code',
					needSort: true
				},
				{
					label: '型号',
					key: 'model',
					needSort: true
				},
				{
					label: '颜色',
					key: 'color'
				},
				{
					label: '入库日期',
					key: 'rkTime',
					isDate: true
				}
			]
		};
	},
	onPageScroll(e) {
		this.scrollTop = e.scrollTop;
	},
	onReady() {
		this.initData();
	},
	methods: {
		...mapActions({
			getProductList: 'storageOrder/getProductList'
		}),
		initData() {
			this.getProductList()
				.then(res => {
					console.log(res);
					if (res && res.code == 0) {
						let originList = [];
						res.data.list.forEach(e => {
							if (e.rkZt == 1 && e.xsZt != 1 && e.ckZt != 1) {
								originList.push(e);
							}
						});
						originList.sort((a, b) => {
							if (a.rkTime && b.rkTime) {
								return new Date(b.rkTime.replace(/\-/g, '/')).getTime() - new Date(a.rkTime.replace(/\-/g, '/')).getTime();
							} else if (a.rkTime) {
								return -1;
							} else {
								return 1;
							}
						});
						this.originList = originList;
						this.list = JSON.parse(JSON.stringify(originList));
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		searchClick() {
			let originList = this.originList;
			let listCache = [];
			if (this.search.type) {
				originList.forEach(e => {
					if (e.model && e.model.includes(this.search.type)) {
						listCache.push(e);
					}
				});
			} else {
				listCache = originList;
			}
			this.list = JSON.parse(JSON.stringify(listCache));
		},
		sortChange(list) {
			this.list = list;
		}
	}
};
</script>

<style lang="less">
@import './styles/queryLayout.less';
</style>
